package cn.wgx.module.mind.service;

import cn.wgx.common.entity.mind.MindBeatContent;
import cn.wgx.common.entity.mind.MindBeatContentHis;
import cn.wgx.module.mind.entity.mapper.BeatContentMapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

@Service
public class BeatContentService extends BaseMindService<BeatContentMapper, MindBeatContent> {

    @Autowired
    private BeatContentHisService beatContentHisService;

    public boolean updateById(MindBeatContent entity) {
        boolean b = SqlHelper.retBool(getBaseMapper().updateById(entity));
        if(b){
            entity = getById(entity.getBeat_id());
            //保存副本数据至历史表
            MindBeatContentHis hisContent = new MindBeatContentHis();
            BeanUtils.copyProperties(entity, hisContent);
            hisContent.setCreateDate(null);
            hisContent.setUpdateDate(null);
            boolean save = beatContentHisService.save(hisContent);

        }
        return b;
    }


    @Transactional()
    public boolean applyHis(Integer beat_id, Integer beat_his_id) {
        MindBeatContentHis hisContent = beatContentHisService.getById(beat_his_id);
        if(hisContent != null && beat_id.equals(hisContent.getBeat_id())){
            MindBeatContent mindBeatContent = new MindBeatContent();
            BeanUtils.copyProperties(hisContent, mindBeatContent);
            boolean b = updateById(mindBeatContent);
            return b;
        }
        return false;
    }
//    @Override
//    @Transactional()
//    public boolean save(MindBeatContent mindBeatContent){
//        return super.save((mindBeatContent));
//    }

//    @Override
//    @Transactional()
//    public boolean saveBatch(Collection<MindBeatContent> mindBeatContent){
//        return super.saveBatch((mindBeatContent));
//    }

//    public MindBeatContent ini(MindBeatContent mindBeatContent){
//        MindBeat byId = beatService.getById(mindBeatContent.getBeat_id());
//        mindBeatContent.setPro_id(byId.getPro_id());
//        return mindBeatContent;
//    }
//
//    public Collection<MindBeatContent> ini(Collection<MindBeatContent> mindBeatContent){
//        for(MindBeatContent mindBeatContent1: mindBeatContent){
//            ini(mindBeatContent1);
//        }
//        return mindBeatContent;
//    }
}
